﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Bouyei.Geo.GeoParsers.GeoJson
{
    public class PointArray :IArray<double>
    {
        private int position = 0;

        private double[] Array;

        public PointArray()
        {
            Array = new double[2];
            
        }

        public PointArray(double x, double y)
        {
            Array = new double[2];
            Array[0] = x;
            Array[1] = y;
        }

        public void FromWkt(string wkt)
        {
            Wkt.GeometryReader parser = new Wkt.GeometryReader(wkt);

            var ls = parser.ReaderCoodinates();
            if (ls.Count == 0) return;

            var coords = ls[0][0][0];
            Array[0] = coords.X;
            Array[1] = coords.Y;
        }

        public object Current => Array[position];

        public double this[int index]
        {
            get { return Array[index]; }
            set { Array[index] = value; }
        }

        public bool MoveNext()
        {
            ++position;
            return position < Array.Length;
        }

        public void Reset()
        {
            position = 0;
        }

        public IEnumerator<double> GetEnumerator()
        {
            foreach (var item in Array)
                yield return item;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            foreach (var item in Array)
                yield return item;
        }
    }
}
